package com.zs.studentmanager.controller;

import com.zs.studentmanager.common.ResponseCodeEnum;
import com.zs.studentmanager.common.Result;
import com.zs.studentmanager.entity.requestParam.ScoreParams;
import com.zs.studentmanager.entity.vo.PageVo;
import com.zs.studentmanager.entity.vo.ScoreVo;
import com.zs.studentmanager.service.StudentScoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 周小白
 * @date: 2024-01-22 21:01
 **/
@RestController
@RequestMapping("/score")
@Tag(name = "成绩管理")
@Slf4j
public class ScoreController {

    private final StudentScoreService studentScoreService;

    public ScoreController(StudentScoreService studentScoreService) {
        this.studentScoreService = studentScoreService;
    }




    /**
     * 获取成绩列表
     * @param scoreParams 参数
     * @return  成绩列表
     */
    @PostMapping("/list")
    @Operation(summary = "获取成绩列表")
    public Result getScoreList(@RequestBody ScoreParams scoreParams) {
        log.info(scoreParams.toString());
        if (scoreParams.getPageNum() == null || scoreParams.getPageSize() == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }
        if (scoreParams.getPageNum() < 1 || scoreParams.getPageSize() < 1) {
            return Result.fail("参数错误");
        }

        PageVo<ScoreVo> pageVo = studentScoreService.getScoreList(
                scoreParams.getPageNum(), scoreParams.getPageSize(), scoreParams.getSearchValue(),
                scoreParams.getCourseId(), scoreParams.getGradeClassId()
        );
        if (pageVo == null) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success(pageVo);
    }


    /**
     * 删除成绩
     * @param id 成绩id
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除成绩")
    public Result deleteScore(@PathVariable Long id) {
        if (id == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        boolean result = studentScoreService.removeById(id);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }

}
